package com.joysuch.wwyt.mh.utils;

import cn.hutool.core.date.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期->季度
 * 日期-> 周
 */
public class TaskCycleUtil {

    /**
     * 获取指定月份中的第week个周一的日期
     * @param year
     * @param month
     * @param week
     * @return
     */
    public static Date getWeekOnMonth(int year, int month, int week) {
        month -= 1;
        Calendar cal = Calendar.getInstance();
        cal.set(year, month,1,0,0,0);
        cal.set(Calendar.MILLISECOND, 0);
        Date firstDayOfMonth = cal.getTime();
        return getSpecifyMonday(firstDayOfMonth,week);
    }

    /**
     * 获取指定月份的所有周一所在的日期
     * @param year
     * @param month
     * @return
     */
    public static List<Date> getAllMonday(int year, int month) {
        month -= 1;
        List<Date> mondayList = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.set(year, month,1,0,0,0);
        cal.set(Calendar.MILLISECOND, 0);
        Date firstDayOfMonth = cal.getTime();
        Date monday = getFirstMonday(firstDayOfMonth);
        mondayList.add(monday);

        while (true) {
            monday = DateUtil.offsetDay(monday, 7);
            if (!DateUtil.isSameMonth(monday, firstDayOfMonth)) {
                break;
            }
            mondayList.add(monday);
        }
        return mondayList;
    }

    /**
     * 获取指定年份的上半年或下半年的开始日期
     *
     * @param year
     * @param period
     * @return
     */
    public static Date getHalfYearInYear(int year, int period) {
        Calendar cal = Calendar.getInstance();
        int month = period == 1 ? 0 : 6;
        cal.set(year, month, 1, 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取指定年份的第几月的开始日期
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getMonthInYear(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month > 0 ? month - 1 : month, 1, 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取指定年份的第season季度的开始日期
     *
     * @param year
     * @param season
     * @return
     */
    public static Date getSeasonInYear(int year, int season) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, 0, 1, 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date firstSeason = cal.getTime();
        return DateUtil.offsetMonth(firstSeason, (season - 1) * 3);
    }

    public static List<Date> getAllSeasonStart(int year) {
        List<Date> allSeasonStart = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.set(year, 0, 1, 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        // 第一季度
        allSeasonStart.add(cal.getTime());
        cal.set(year, 3, 1);
        // 第二季度
        allSeasonStart.add(cal.getTime());
        cal.set(year, 6, 1);
        // 第三季度
        allSeasonStart.add(cal.getTime());
        cal.set(year, 9, 1);
        // 第四季度
        allSeasonStart.add(cal.getTime());
        return allSeasonStart;
    }

    public static List<Date> getAllMonthStart(int year) {
        List<Date> monthFirstDayList = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.set(year, 0, 1, 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date firstDayOfMonth = cal.getTime();
        monthFirstDayList.add(firstDayOfMonth);

        while (true) {
            firstDayOfMonth = DateUtil.offsetMonth(firstDayOfMonth, 1);
            Calendar c = Calendar.getInstance();
            c.setTime(firstDayOfMonth);
            int nextYear = c.get(Calendar.YEAR);
            if (nextYear != year) {
                break;
            }
            monthFirstDayList.add(firstDayOfMonth);
        }
        return monthFirstDayList;
    }

    public static List<WeekRange> getWeekRangeOnMonth(int year, int month) {
        ArrayList<WeekRange> weekRanges = new ArrayList<>();
        month -= 1;
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, 1, 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date firstDay = cal.getTime();
        Date monday = getFirstMonday(firstDay);

        while (DateUtil.isSameMonth(monday, firstDay)) {
            weekRanges.add(new WeekRange(new Date(monday.getTime())));
            monday = DateUtil.offsetWeek(monday, 1);
        }
        return weekRanges;
    }

    private static Date getSpecifyMonday(Date date, int week) {
        date = getFirstMonday(date);
        return DateUtil.offsetDay(date, 7 * (week - 1));
    }

    /**
     * 获取当前月中的第一个周一
     */
    private static Date getFirstMonday(Date date) {
        int dayOfWeek = DateUtil.dayOfWeek(date);
        if (dayOfWeek == 2) {
            return date;
        }
        if (dayOfWeek == 1) {
            return DateUtil.offsetDay(date, 1);
        }
        return DateUtil.offsetDay(date, 9 - dayOfWeek);
    }

    public static List<Date> getAllHalfYearStart(Integer year) {
        List<Date> halfYearFirstDayList = new ArrayList<>();
        Date halfYear1StartTime = TaskCycleUtil.getHalfYearInYear(year, 1);
        Date halfYear2StartTime = TaskCycleUtil.getHalfYearInYear(year, 2);
        halfYearFirstDayList.add(halfYear1StartTime);
        halfYearFirstDayList.add(halfYear2StartTime);
        return halfYearFirstDayList;
    }
}
